package zpin.action;

import java.util.ArrayList;
import java.util.List;

import util.dao.Pagination;
import zpin.dao.CityDao;
import zpin.model.City;

import com.opensymphony.xwork2.ActionSupport;

public class CityAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	
	private City city;
	
	private Long id;
	
	private CityDao cityDao;
	
	List<City> citylist = new ArrayList<City>();
	
	private Pagination<City> pagination;

	
	public List<City> getCitylist() {
		return citylist;
	}

	public void setCitylist(List<City> citylist) {
		this.citylist = citylist;
	}

	public Pagination<City> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<City> pagination) {
		this.pagination = pagination;
	}

	public City getCity() {
		return city;
	}

	public void setCity(City city) {
		this.city = city;
	}

	public CityDao getCityDao() {
		return cityDao;
	}

	public void setCityDao(CityDao cityDao) {
		this.cityDao = cityDao;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public String create(){
		cityDao.create(city);
		
		return SUCCESS;
	}
	
	public String show(){
		if (pagination == null) {
			this.pagination = new Pagination<City>();
		}

		if (this.pagination.getSize() == 0) {
			this.pagination.setSize(5);
		}
		if (this.pagination.getNo() == 0) {
			this.pagination.setNo(1);
		}
		
		cityDao.browse(pagination);
		citylist = pagination.getResults();
		pagination.setOrderFieldName("name");
		
		return SUCCESS;
	}
	
	public String delete(){
		cityDao.delete(city);
		return SUCCESS;
	}
	
	public String preupdate(){
		city = cityDao.findById(id);
		return SUCCESS;
	}
	
	public String update(){
		cityDao.update(city);
		
		return SUCCESS;
	}
}
